<template>
  <div>
    <MyHeader title="购物车案例"></MyHeader>
    <div class="main">
      <!-- <MyGoods v-for="obj in list" :key="obj.id" :gObj="obj"></MyGoods> -->

      <!-- 开启命名空间 -->
      <!-- <MyGoods
        v-for="obj in $store.state.cart.goodsList"
        :key="obj.id"
        :gObj="obj"
      ></MyGoods> -->
      <MyGoods v-for="(obj,index) in list" :key="obj.id" :gObj="obj" :i='index'></MyGoods>
    </div>
    <MyFooter @changeAll="allFn" :arr="list"></MyFooter>
  </div>
</template>

<script>
import MyHeader from "./components/MyHeader";
import MyGoods from "./components/MyGoods";
import MyFooter from "./components/MyFooter";
import { mapState, mapActions } from "vuex";
export default {
  components: {
    MyHeader,
    MyGoods,
    MyFooter,
  },
  computed: {
    // ...mapState({
    //   list: "goodsList",
    // }),
    ...mapState("cart", { list: "goodsList" }),
  },

  created() {
    //因为打开就会获取数据
    this.asyncSetGoodList(); //调用调用调用调用调用，记得()
  },
  methods: {
    ...mapActions("cart", ["asyncSetGoodList"]),
    allFn(bool) {
      this.list.forEach((obj) => (obj.goods_state = bool));
    },
  },
};
</script>

<style scoped>
.main {
  padding-top: 45px;
  padding-bottom: 50px;
}
</style>